package com.yunji.ylexpiration.Controller;

import com.yunji.ylexpiration.entity.FurnAddresstable;
import com.yunji.ylexpiration.service.AddaddressService;
import com.yunji.ylexpiration.util.SimpleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class FurnAddresstableController {
    @Autowired
    private AddaddressService addaddressService;
    @PostMapping("/site")
    @ResponseBody
    public SimpleResponse insertSelective(String consignee,String phone,String province,String city,String area,String detailed,String address,int userId){
        try {
            FurnAddresstable furnAddresstable=new FurnAddresstable();
            furnAddresstable.setConsignee(consignee);
            furnAddresstable.setPhone(phone);
            furnAddresstable.setProvince(province);
            furnAddresstable.setCity(city);
            furnAddresstable.setArea(area);
            furnAddresstable.setDetailed(detailed);
            furnAddresstable.setAddress(address);
            furnAddresstable.setUserId(userId);
                int a=addaddressService.insertSelective(furnAddresstable);
                if (a>0){
                    return SimpleResponse.ok(a);
                }else {
                    return SimpleResponse.ok(null);
                }
        }catch (Exception e){
            e.printStackTrace();
            return SimpleResponse.error(5555,"新增错误","详情看上面");
        }
    }
@GetMapping("address2.html")
@ResponseBody
public List<FurnAddresstable>furnAddresstableList(Integer userId){
        List<FurnAddresstable>furnAddresstables=addaddressService.seleuid(userId);
        return furnAddresstables;
}
@GetMapping("/delid/{id}")
@ResponseBody
    private SimpleResponse delid(@PathVariable Integer id){
      try {
          int regions=addaddressService.deleteByPrimaryKey(id);
          return SimpleResponse.ok(regions);
      }catch (Exception e){
          return SimpleResponse.error(9999,"删除异常","select Region");
      }
}
@GetMapping("/update")
@ResponseBody
    private SimpleResponse update(HttpServletRequest request){
    try {
        FurnAddresstable furnAddresstable=new FurnAddresstable();
        //id条件
        furnAddresstable.setId(Integer.valueOf(request.getParameter("pid")));
        //姓名
        furnAddresstable.setConsignee(request.getParameter("a1"));
        //电话
        furnAddresstable.setPhone(request.getParameter("a2"));
        //省市区街道
        furnAddresstable.setProvince(request.getParameter("a3"));
        furnAddresstable.setCity(request.getParameter("a4"));
        furnAddresstable.setArea(request.getParameter("a5"));
        furnAddresstable.setDetailed(request.getParameter("a6"));
        //配送地址
        furnAddresstable.setAddress(request.getParameter("a7"));
         int i = addaddressService.updateByPrimaryKeySelective(furnAddresstable);
        return SimpleResponse.ok(i);
    }catch (Exception e){
        return SimpleResponse.error(9999,"修改错误","select Region");
    }
}
@GetMapping("/order")
@ResponseBody
public List<FurnAddresstable>order(Integer userId){
    List<FurnAddresstable>furnAddresstables=addaddressService.seleuid(userId);
    return furnAddresstables;
}

}
